/*
 * $Id: PlanFirstCategoryItemAction.java 1724 2011-08-30 09:14:20Z abie $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.cloth.web.planfirst;

import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.core.common.treegrid.GridConstants;
import com.lightsaber.trade.core.web.BaseAction;
import com.lightsaber.trade.cloth.entity.category.Category;
import com.lightsaber.trade.cloth.entity.planning.Level1Planning;
import com.lightsaber.trade.cloth.service.planfirst.CategoryPlanningItemManager;
import com.lightsaber.trade.cloth.service.planfirst.PlanFirstLevelManager;

@Namespace("/planfirst")
public class PlanFirstCategoryItemAction extends BaseAction implements SessionAware {

    private static final long serialVersionUID = 3655594228037485703L;

    protected Map<String, Object> session;

    @Override
    public void setSession(final Map<String, Object> session) {
        this.session = session;
    }

    @Autowired
    private transient PlanFirstLevelManager planFirstLevelManager;

    private String treeGridData;
    private String level1PlanningId;

    private Level1Planning level1Planning;
    private Operator operator;

    private Category category;

    @Autowired
    private transient CategoryPlanningItemManager categoryPlanningItemManager;

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        level1Planning = planFirstLevelManager.get(level1PlanningId);
        category = level1Planning.getCategory();
        setLevel1Planning(level1Planning);
        return SUCCESS;
    }

    public String data() throws Exception {
        String role = (String) session.get(ROLE_CODE);

        treeGridData = categoryPlanningItemManager.generateTreeGrid(level1PlanningId, role,
                operator);
        Struts2Utils.renderXml(treeGridData);
        return null;

    }

    public String save() throws Exception {
        categoryPlanningItemManager.save(treeGridData);
        Struts2Utils.renderXml(GridConstants.SUCESSFULLY);
        addActionMessage(getText(PROCESSED_SUCCESS));
        return null;
    }

    public String getTreeGridData() {
        return treeGridData;
    }

    public void setTreeGridData(final String treeGridData) {
        this.treeGridData = treeGridData;
    }

    public String getLevel1PlanningId() {
        return level1PlanningId;
    }

    @Override
    public String input() throws Exception {
        return SUCCESS;
    }

    public void setLevel1PlanningId(final String level1PlanningId) {
        this.level1PlanningId = level1PlanningId;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(final Operator operator) {
        this.operator = operator;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(final Category category) {
        this.category = category;
    }

    public Level1Planning getLevel1Planning() {
        return level1Planning;
    }

    public void setLevel1Planning(final Level1Planning level1Planning) {
        this.level1Planning = level1Planning;
    }

}
